<?php

use Biz\model\brandActivity\BlindBoxActivity as mBlindBoxActivity;
use Biz\service\brand\BlindBox;
use Biz\service\brand\Brand;
use Doraemon\service\Follow;

/**
 * 品牌列表
 */
class BrandListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $now = time();
        $where = [
            'activity_state' => mBlindBoxActivity::ACTIVITY_STATE_PASS,
            'start_time[<=]' => $now,
            'end_time[>=]' => $now,
            'brand_id[>]' => 0,
            'ORDER' => ['prize_total_price' => 'DESC'],
        ];
        $brandIds = mBlindBoxActivity::selectByWhere($where, 'brand_id');
        $brandIds = array_unique($brandIds);
        $brandInfos = Brand::getBrandListByIds4App($brandIds);
        $uids = array_column($brandInfos, 'uid');
        $followStatus = Follow::isFollowedMulti($this->uid, $uids);
        $list = [];
        foreach ($brandInfos as $row) {
            $list[] = [
                'id' => $row['id'],
                'uid' => $row['uid'],
                'name' => $row['name'],
                'logo' => $row['logo'],
                'is_followed' => isset($followStatus[$row['uid']]) ? $followStatus[$row['uid']] : 0,
                'point' => BlindBox::TASK_FOLLOW_BRAND_POINT
            ];
        }
        if ($list) {
            $sortFollowed = array_column($list, 'is_followed');
            array_multisort($sortFollowed, SORT_ASC, $list);
        }
        $this->setSuccess(['list' => $list]);
    }
}